/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Board;
import Model.Constants;
import Model.Position;
import View.BoardInterface;
import View.ChooseMode;
import View.Creater;
import View.GameInterface;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.NoRouteToHostException;
import javax.swing.JOptionPane;

/**
 *
 * @author Long
 */
public class CreaterListener extends Controller implements ActionListener {

    public CreaterListener(BoardInterface boardInterface, Board board, GameInterface gameInterface, ChooseMode chooseMode, Creater creater) {
        super(boardInterface, board, gameInterface, chooseMode, creater);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        boardInterface.getMoveLabel().setText("");
        boardInterface.getTimeLabel().setText("");
        boardInterface.getTotalMoveLabel().setText("");

        final String home = creater.getNameFeild().getText();
        if (!home.equals("")) {
            if (event.getSource() == creater.getCreateGameButton()) {
                createHost(home);
            } else if (event.getSource() == creater.getJoinGameButton()) {
                createClient(home);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Xin hãy nhập tên người chơi");
        }
    }

    public void createHost(final String home) {
        creater.setVisible(false);
        creater.setTypeOfPlayer(Constants.HOST);
        isLocked = true;
        boardInterface.setPlayer1Name(home);
        boardInterface.setVisible(true);
        Thread optionThead = new Thread() {
            @Override
            public void run() {
                boardInterface.setPlayer2Name("Xin hãy chờ người chơi thứ 2 vào trò chơi");
                optionTranfer = new SampleTranfer(null, Constants.PORT_OPTION, Constants.HOST);
                String namePlayer2 = optionTranfer.receiveData();
                optionTranfer.sendData(home);
                boardInterface.setPlayer2Name(namePlayer2);
                boardInterface.setPlayer1Icon(iconTurn);
                boardInterface.setPlayer2Icon(null);
                isLocked = false;
            }
        };
        optionThead.start();
        Thread sendThread = new Thread() {
            public void run() {
                sendTranfer = new SampleTranfer(null, Constants.PORT_SEND_BY_SERVER, Constants.HOST);
            }
        };
        sendThread.start();
        Thread recvThread = new Thread() {
            public void run() {
                recvTranfer = new SampleTranfer(null, Constants.PORT_RECEIVE_BY_SERVER, Constants.HOST);
                while (true) {
                    Position pos = new Position(recvTranfer.receiveData());
                    boardInterface.setArrayOfButton(pos, icon2);
                    board.updateBoard(pos, Constants.COMPUTER);
                    boardInterface.setPlayer1Icon(iconTurn);
                    boardInterface.setPlayer2Icon(null);
                    isLocked = false;
                    if (CheckWinner.winner(board.getBoardCell(), pos.getX(), pos.getY(), 5) == 2) {
                        optionTranfer.sendData(Constants.WINNER);
                        JOptionPane.showMessageDialog(null, "Bạn đã thua");
                        boardInterface.setPlaye2Score(boardInterface.getPlayer2Score() + 1);
                        board.resetBoard();
                        boardInterface.setDefaultBoard();
                    }
                }
            }
        };
        recvThread.start();
    }

    public void createClient(final String home) {
        creater.setVisible(false);
        isLocked = true;
        String ip = JOptionPane.showInputDialog("Xin mời nhập IP của máy chủ");
        creater.setIpHost(ip);
        creater.setTypeOfPlayer(Constants.CLIENT);
        boardInterface.setPlayer2Name(home);
        boardInterface.getNewGameButton().setVisible(false);
        boardInterface.setVisible(true);
        

        Thread optionThread = new Thread() {
            public void run() {
                optionTranfer = new SampleTranfer(creater.getIpHost(), Constants.PORT_OPTION, Constants.CLIENT);
                optionTranfer.sendData(home);
                boardInterface.setPlayer1Name(optionTranfer.receiveData());
                boardInterface.setPlayer1Icon(iconTurn);
                boardInterface.setPlayer2Icon(null);
                while (true) {
                    String recvData = optionTranfer.receiveData();
                    if (recvData.equals(Constants.WINNER)) {
                        JOptionPane.showMessageDialog(null, "Bạn đã dành chiến thắng");
                        boardInterface.setPlayer1Score(boardInterface.getPlayer1Score() + 1);
                        boardInterface.setDefaultBoard();
                        board.resetBoard();
                    } else if (recvData.equals(Constants.LOSE)) {
                        JOptionPane.showMessageDialog(null, "Bạn đã thua");
                        boardInterface.setPlaye2Score(boardInterface.getPlayer2Score() + 1);
                        boardInterface.setDefaultBoard();
                        board.resetBoard();
                    } else if (recvData.equals(Constants.NEW_GAME)) {
                        int choose = JOptionPane.showConfirmDialog(null, "Người chơi 1 muốn tiến hành ván chơi mới . Bạn có đồng ý ?");
                        if (choose == JOptionPane.YES_OPTION) {
                            optionTranfer.sendData(Constants.ACCEPT);
                            boardInterface.setDefaultBoard();
                            board.resetBoard();
                        } else {
                            optionTranfer.sendData(Constants.IGNORE);
                        }
                    }
                }

            }
        };
        optionThread.start();

        sendTranfer = new SampleTranfer(creater.getIpHost(), Constants.PORT_RECEIVE_BY_SERVER, Constants.CLIENT);

        Thread recvThread = new Thread() {
            public void run() {
                recvTranfer = new SampleTranfer(creater.getIpHost(), Constants.PORT_SEND_BY_SERVER, Constants.CLIENT);
                while (true) {
                    Position pos = new Position(recvTranfer.receiveData());
                    boardInterface.setArrayOfButton(pos, icon1);
                    board.updateBoard(pos, Constants.HUMAN);
                    boardInterface.setPlayer2Icon(iconTurn);
                    boardInterface.setPlayer1Icon(null);
                    isLocked = false;
                }
            }
        };
        recvThread.start();
    }
}
